package com.fantasy.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fantasy.blog.dao.mapper.SysUserMapper;
import com.fantasy.blog.dao.pojo.SysUser;
import com.fantasy.blog.service.LoginService;
import com.fantasy.blog.service.ProxyService;
import com.fantasy.blog.service.SysUserService;
import com.fantasy.blog.utils.JWTUtils;
import com.fantasy.blog.vo.ErrorCode;
import com.fantasy.blog.vo.LoginUserVo;
import com.fantasy.blog.vo.Result;
import com.fantasy.blog.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private LoginService loginService;
    @Autowired
    private ProxyService proxyService;

    @Override
    public SysUser findUserById(Long id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public UserVo findUserVoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if(null == sysUser){
            sysUser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/user/user_1.png");
            sysUser.setNickname("FantasyBLOG");
        }

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        return userVo;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(SysUser::getAccount,account)
                .eq(SysUser::getPassword,password)
                .select(SysUser::getId,SysUser::getAccount,SysUser::getAvatar,SysUser::getNickname)
                .last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Result findUserByToken(String token) {
        SysUser sysUser = loginService.checkToken(token);
        if(null==sysUser){
            return Result.fail(ErrorCode.TOKEN_ILLEAGL.getCode(), ErrorCode.TOKEN_ILLEAGL.getMsg());
        }
        LoginUserVo loginUserVo = new LoginUserVo();
        BeanUtils.copyProperties(sysUser,loginUserVo);
        return Result.success(loginUserVo);
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getPassword,account)
                .select()
                .last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
    }

    @Override
    public SysUser findUserByQQ(String qq) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getQq,qq)
                .select(SysUser::getId
                        ,SysUser::getNickname
                        ,SysUser::getAvatar)
                .last("limit 1");

        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        return sysUser;
    }

    @Override
    public SysUser creatUserByQQ(String qq, String email) throws IOException {
        SysUser sysUser;

        Map<String, String> qqObject = proxyService.getQQObject(qq);

        String nickname = qqObject.get("nickname");
        String avatar = qqObject.get("avatar");

        sysUser = new SysUser();
        sysUser.setAccount(qq);
        sysUser.setNickname(nickname);
        sysUser.setQq(qq);
        sysUser.setEmail(email);
        sysUser.setCreateDate(System.currentTimeMillis());
        sysUser.setLastLogin(System.currentTimeMillis());
        sysUser.setAvatar(avatar);
        sysUser.setDeleted(0); // 0 为false
        sysUser.setSalt("");
        sysUser.setStatus("");
        sysUser.setPassword("!@#$%^&*()_+ERTYUI");
        this.save(sysUser);

        return sysUser;
    }
}
